package com.zhuchaooasystem.ui.home.fragment.oa.daily;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.adapter.holder.DailyListHolder;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.base.BaseEvent;
import com.zhuchaooasystem.common.Key;
import com.zhuchaooasystem.entity.DailyEntity;
import com.zhuchaooasystem.presenter.home.fragment.oa.daily.DailyContract;
import com.zhuchaooasystem.presenter.home.fragment.oa.daily.DailyPresenter;
import com.zhuchaooasystem.utils.AppUtils;
import com.zhuchaooasystem.utils.ListUtils;
import com.zhuchaooasystem.utils.NetWorkUtil;
import com.zhuchaooasystem.utils.ScreenUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class DailyActivity extends BaseActivity<DailyPresenter> implements DailyContract.View, RecyclerArrayAdapter.OnMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.actionbar_txt_title)
    TextView txt_title;
    @BindView(R.id.actionbar_iv_back)
    ImageView iv_back;
    @BindView(R.id.actionbar_txt_right)
    TextView txt_right;
    //----
    @BindView(R.id.act_daily_recycle)
    EasyRecyclerView recycle;

    private int index = 1;
    private List<DailyEntity.ResultBean.SuccessfulBean> dailyList;
    private RecyclerArrayAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_daily;
    }

    @Override
    protected DailyPresenter getPresenter() {
        return new DailyPresenter(mContext, this);
    }

    @Override
    protected void initVariables() {
        index = 1;
        RxBus.get().register(this);
    }


    @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag("newDaily")})
    public void newDaily(BaseEvent baseEvent) {
        if (null != baseEvent) {
            onRefresh();
        }
    }

    @Override
    protected void initView() {
        setActionBar();
        //----
        initRecycle();
    }

    private void initRecycle() {
        recycle.setRefreshingColor(Key.RECYCLEVIEW_REFRESH_COLORS);
        recycle.setLayoutManager(new LinearLayoutManager(mContext));
        DividerDecoration itemDecoration = new DividerDecoration(getResources().getColor(R.color.color_e1e1e1), ScreenUtils.dip2px(this, 1f), 0, 0);
        itemDecoration.setDrawLastItem(false);
        recycle.addItemDecoration(itemDecoration);
        recycle.setAdapterWithProgress(adapter = new RecyclerArrayAdapter<DailyEntity.ResultBean.SuccessfulBean>(mContext) {

            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new DailyListHolder(parent);
            }
        });
        recycle.setEmptyView(R.layout.view_empty);
        //----
        recycle.setRefreshListener(this);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore, new RecyclerArrayAdapter.OnNoMoreListener() {
            @Override
            public void onNoMoreShow() {
                adapter.resumeMore();
            }

            @Override
            public void onNoMoreClick() {
                adapter.resumeMore();
            }
        });
        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });
    }

    private void setActionBar() {
        txt_title.setText(getString(R.string.actionbar_title_daily));
        txt_right.setText("新建");
        txt_right.setVisibility(View.VISIBLE);
        iv_back.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initDataAndLoadData() {
        //----
        onRefresh();
    }

    @Override
    public void refreshData(List<DailyEntity.ResultBean.SuccessfulBean> data) {
        //测试
        this.dailyList = data;
        adapter.clear();
        adapter.addAll(data);
    }

    @Override
    public void loadMoreData(List<DailyEntity.ResultBean.SuccessfulBean> data) {
        this.dailyList = ListUtils.listPlus(dailyList, data);
        adapter.addAll(data);
    }

    @OnClick({R.id.actionbar_iv_back, R.id.actionbar_txt_right})
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.actionbar_iv_back:
                finish();
                break;
            //新建
            case R.id.actionbar_txt_right:
                AppUtils.jump(mContext, NewDailyActivity.class);
                break;
        }
    }

    @Override
    public void onRefresh() {
        //刷新
        if (!NetWorkUtil.isNetConnected(mContext)) {
            adapter.pauseMore();
            return;
        }
        index = 1;
        mPresenter.getDailyList(index);
    }

    @Override
    public void onMoreShow() {
        index++;
        mPresenter.getDailyList(index);
    }

    @Override
    public void onMoreClick() {
        mPresenter.getDailyList(index);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister(this);
    }

}
